<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class Router
{
	const DEFAULT_ACTION = 'index';
	const DEFAULT_METHOD = 'view';
	
	private
		$action		= '',
		$method		= '',
		$directory 	= '';
	
	private static $params = array();
	
	public function __construct()
	{
		$uriString = $this->getUriString();
		
		if ($uriString == '')
		{
			$this->setAction(self::DEFAULT_ACTION);
			$this->setMethod(self::DEFAULT_METHOD);
		}
		else
		{
			// Split into controller -> method
			$parts = explode('/', $uriString);
			
			// Check for page extension
			$last = end($parts);
			
			if (strpos($last, Config::get('page_extension')) !== false)
			{
				$parts[key($parts)] = str_replace('.' . Config::get('page_extension'), '', $last);
			}
			
			// Check action is valid
			$parts = $this->validateRequest($parts);
			
			$this->setAction($parts[0]);
			$this->setMethod($parts[1]);
			
			self::$params = array_slice($parts, 2);
		}
	}
	
	private function getUriString()
	{
		$uri = '';
		
		// Style: index.php?topic/view/1
		if (!empty($_GET) && count($_GET) == 1)
		{
			$uri = key($_GET);
		}
		
		// Style: index.php/topic/view/1
		$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
		if ($path != '')
		{
			$uri = $path;
		}
		
		// Style: index.php?page=topic&act=view&id=1
		$path = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
		if ($path != '')
		{
			$parts = explode('&', $path);
			
			foreach ($parts as $part)
			{
				$part = explode('=', $part);
				
				$uri .= $part[1] . '/';
			}
		}
		
		return trim($uri, '/');
	}
	
	private function validateRequest($parts)
	{
		$actionPath = (!defined('NOVA_ADMIN')) ? 'engine/actions/' : 'engine/actions/admin/';
		
		// Does the requested controller exist?
		if (file_exists(ROOT . $actionPath . $parts[0] . '.action.php'))
		{
			if (!isset($parts[1]))
			{
				$parts[1] = self::DEFAULT_METHOD;
			}
			
			return $parts;
		}
		
		// Check if it's in a sub-folder
		if (is_dir(ROOT . $actionPath . $parts[0]))
		{
			$dir = $this->setDirectory($parts[0]);
			
			// Remove dir from array
			array_shift($parts);
			
			if (file_exists(ROOT . $actionPath . $dir . $parts[0] . '.action.php'))
			{
				if (!isset($parts[1]))
				{
					$parts[1] = self::DEFAULT_METHOD;
				}
				
				return $parts;
			}
		}
		
		throw new Exception('Action \'' . $parts[0] . '\' could not be located!');
	}
	
	public static function param($key)
	{
		return isset(self::$params[$key]) ? self::$params[$key] : false;
	}
	
	public function getAction()
	{
		return $this->action;
	}
	
	public function getMethod()
	{
		return $this->method;
	}
	
	public function getDirectory()
	{
		return $this->directory;
	}
	
	private function setAction($action)
	{
		$this->action = $action;
	}
	
	private function setMethod($method)
	{
		$this->method = $method;
	}
	
	private function setDirectory($dir)
	{
		return $this->directory = $dir . '/';
	}
}

?>